package com.dtsz.m2.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.HomeVo;
import com.dtsz.m2.utils.PackageUtils;
import com.dtsz.m2.volley.state.HUCState;


public class DownActivity extends BaseActivity {
    private TextView tvVersion, tvContent, tvLabel;
    private Button btLoad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_down);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("版本更新");
        getVersion();
    }

    @Override
    public void initView() {
        super.initView();
        tvVersion = findViewById(R.id.tv_version);
        tvContent = findViewById(R.id.tv_content);
        tvLabel = findViewById(R.id.tv_label);
        btLoad = findViewById(R.id.bt_load);
    }

    @Override
    public void initClick() {
        super.initClick();
        btLoad.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_load:
                String url = "http://www.m2.app"; // web address
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof HomeVo) {
                    HomeVo homeVo = (HomeVo) response;
                    String version = homeVo.getContent();
                    String usersionC = PackageUtils.getAppVersionName(this);
                    tvVersion.setText("当前版本:" + usersionC);
                    version = version.split(",")[0];
                    tvLabel.setText("最新版本:" + version);
                    if (!usersionC.equals(version)) {
                        tvContent.setText(homeVo.getTitle());
                        tvContent.setVisibility(View.VISIBLE);
                        btLoad.setVisibility(View.VISIBLE);
                    } else {
                        tvContent.setVisibility(View.GONE);
                        btLoad.setVisibility(View.GONE);
                    }
                }
                break;
        }
        loadDismiss();
    }

    private void getVersion() {
        //获取网络数据
        pageVo.setLikeName("钱包");
        hucRequestInterfase.executeStr(this, HomeVo.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.VERSION_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }
}
